package com.fqcar.fqdf.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.entity.CarDetailEntity;
import com.fqcar.fqdf.ui.HistoryCarListActivity;
import com.fqcar.fqdf.ui.NormalHomeActivity;
import com.fqcar.fqdf.util.Constants;
import com.fqcar.fqdf.util.NavigationUtils;
import com.fqdf.framework.android.adapter.BaseMyAdapter;
import com.fqdf.framework.android.util.AndroidUtils;
import com.fqdf.framework.android.util.CheckLogicUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CarListAdapter extends BaseMyAdapter<CarDetailEntity> {

    private Context context;

    public CarListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.adapter_car_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CarDetailEntity carDetailEntity = getItem(position);
        holder.mainCarPlateNum.setText(carDetailEntity.getCarNum());
        holder.mainCarBrand.setText(carDetailEntity.getBrand());
        holder.mainCarState.setText(carDetailEntity.getStatusDesc());
        holder.mainCarMileage.setText(carDetailEntity.getRemainMileageDesc());
        holder.mainCarBattery.setText(carDetailEntity.getPowerDesc());
        holder.mainCarChargeState.setText(carDetailEntity.getChargeStatusDesc());
        holder.mainCarMachineState.setText(carDetailEntity.getOnlineStatusDesc());
        if (carDetailEntity.getLastDeal() != null) {
            /*if(!CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getEndTime())){
                holder.mainCarStateDescTime.setText(carDetailEntity.getLastDeal().getEndTime());
                holder.mainCarStateDescTime.setTextColor(AndroidUtils.getColor(context,R.color.black));
            }else {
                holder.mainCarStateDescTime.setText(context.getString(R.string.manage_doing_txt));
                holder.mainCarStateDescTime.setTextColor(AndroidUtils.getColor(context,R.color.nf26050));
            }*/
            holder.mainCarStateDescTime.setText(Html.fromHtml(carDetailEntity.getLastDeal().getStartTime()));

//            holder.mainCarStateDesc.setText(carDetailEntity.getLastDeal().getCause() + "," + carDetailEntity.getLastDeal().getDetail());

            if (!CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getCause()) && !CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getDetail())) {
                holder.mainCarStateDesc.setText(carDetailEntity.getLastDeal().getCause() + "," + carDetailEntity.getLastDeal().getDetail());
            } else if (!CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getDetail())) {
                holder.mainCarStateDesc.setText(carDetailEntity.getLastDeal().getDetail());
            } else if (!CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getCause())) {
                holder.mainCarStateDesc.setText(carDetailEntity.getLastDeal().getCause());
            } else if (CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getCause()) && CheckLogicUtil.isEmpty(carDetailEntity.getLastDeal().getDetail())) {
                holder.mainCarStateDesc.setText("");
            }

        } else {
            holder.mainCarStateDescTime.setText("");
            holder.mainCarStateDesc.setText("");
        }

        holder.navBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavigationUtils.showNavigationDialog(context, new LatLng(carDetailEntity.getLatitude(), carDetailEntity.getLongitude()),
                        NavigationUtils.Driving, NavigationUtils.ReturnCar, "车辆位置");

            }
        });
        holder.historyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent history_intent = new Intent(context,HistoryCarListActivity.class);
                history_intent.putExtra(Constants.BUNDLE_DATA,carDetailEntity.getCarId());
                history_intent.putExtra(Constants.BUNDLE_DATA_EXT,carDetailEntity.getCarNum());
                context.startActivity(history_intent);
            }
        });


        return convertView;
    }

    static class ViewHolder {

        @BindView(R.id.main_car_plate_num)
        TextView mainCarPlateNum;
        @BindView(R.id.main_car_brand)
        TextView mainCarBrand;
        @BindView(R.id.main_car_state)
        TextView mainCarState;
        @BindView(R.id.main_car_mileage)
        TextView mainCarMileage;
        @BindView(R.id.main_car_battery)
        TextView mainCarBattery;
        @BindView(R.id.main_car_charge_state)
        TextView mainCarChargeState;
        @BindView(R.id.main_car_machine_state)
        TextView mainCarMachineState;
        @BindView(R.id.main_car_state_desc)
        TextView mainCarStateDesc;
        @BindView(R.id.main_car_state_desc_time)
        TextView mainCarStateDescTime;
        @BindView(R.id.nav_btn)
        Button navBtn;
        @BindView(R.id.history_btn)
        Button historyBtn;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
